package api

import (
	"fmt"
	"io/ioutil"
	"runtime/debug"
	"strconv"

	"github.com/deploy-services/v2/KolonseWeb/HttpLib"
	"github.com/deploy-services/v2/KolonseWeb/Type"
	"github.com/deploy-services/v2/services"
)

func service_show(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	rest := services.ServiceShow(req.URL.Query().Get("name"))
	res.Write([]byte(rest))
}

func service_history(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	rest := services.ShowServiceHistory(req.URL.Query().Get("name"))
	res.Write([]byte(rest))
}

func service_commit(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	name := req.URL.Query().Get("name")
	rest := services.ServiceCommit(name)
	res.Write([]byte(rest))
}

func service_active(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	name := req.URL.Query().Get("name")
	commit := req.URL.Query().Get("commit")
	force, _ := strconv.ParseBool(req.URL.Query().Get("force"))
	if len(commit) == 0 {
		commit = "latest"
	}
	rest := services.ServiceActive(name, commit, force)
	res.Write([]byte(rest))
}

func service_start(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	name := req.URL.Query().Get("name")
	node := req.URL.Query().Get("node")
	rest := services.ServiceStart(name, node)
	res.Write([]byte(rest))
}

func service_stop(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	name := req.URL.Query().Get("name")
	node := req.URL.Query().Get("node")
	rest := services.ServiceStop(name, node)
	res.Write([]byte(rest))
}

func service_health(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	name := req.URL.Query().Get("name")
	node := req.URL.Query().Get("node")
	rest := services.ServiceHealth(name, node)
	res.Write([]byte(rest))
}

func service_cmd(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	name := req.URL.Query().Get("name")
	node := req.URL.Query().Get("node")
	buf, _ := ioutil.ReadAll(req.Body)
	defer req.Body.Close()
	rest := services.ServiceCMD(name, node, string(buf))
	res.Write([]byte(rest))
}
